package com.servlet.filter;

import com.servlet.bean.User;
import com.servlet.utils.ResponseVo;
import com.servlet.utils.ServletUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.logging.LogRecord;

/**
 * @Title: ACrosFilter
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/27 11:30
 */
@WebFilter("/*")
public class ACrosFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        // 跨域 设置响应头
        response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        /* 重新预检验跨域的缓存时间 (s) */
        response.setHeader("Access-Control-Max-Age", "3600");
//        /* 允许跨域的请求头  可以是自定义的*/
        response.setHeader("Access-Control-Allow-Headers", "Origin,request-origin,Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,token,X-Token");
        /* 是否携带cookie */
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Expose-Headers", "*");

        filterChain.doFilter(request,response);


    }

    @Override
    public void destroy() {

    }
}
